
import parsedatetime as pdt
import parsedatetime_consts as pdc
from datetime import datetime
import time

class DateExtracter(object):
    constants = None
    parser = None
        
    def __init__(self):
        self.constants = pdc.Constants()
        self.parser = pdt.Calendar(self.constants)

        
    # returns datetime object if date/time is present in source datetimeString
    # else returns null
    # relative dates are adjusted to sourceTime
    def findDate(self, datetimeString, sourceTime=None):
        result = self.parser.parse(datetimeString, sourceTime)

        if result[1] > 0:
            # create date time object
            return datetime(*result[0][0:6])
        else:
            # return null
            return None
            

    # returns datetime object if date/time is present in source datetimeString
    # else returns null
    # relative dates are adjusted to sourceTime
    def extractDates(self, strings, sourceTime=None):
        count = 0
        results = []        
        for str in strings:
            result = self.findDate(str, sourceTime)
            if result is not None:
                count = count + 1
                event = (result, str)
                results.append(event)     

        return results
                
            
###########################
### MAIN
###########################

#c = pdc.Constants()
#p = pdt.Calendar(c)

#print c.uses24, c.usesMeridian      # 24hr clock? AM/PM used?
#print c.usePyICU                    # was PyICU found/enabled?
#print c.meridian                    # list of the am and pm values
#print c.am                          # list of the lowercased and stripped am string
#print c.pm                          # list of the lowercased and stripped pm string
#print c.dateFormats                 # dictionary of available date format strings
#print c.timeFormats                 # dictionary of available time format strings
#print c.timeSep                     # list of time separator, e.g. the ':' in '12:45'
#print c.dateSep                     # list of date serarator, e.g. the '/' in '11/23/2006'
#print c.Months                      # list of full month names
#print c.shortMonths                 # list of the short month names
#print c.Weekdays                    # list of the full week day names
#print c.localeID                    # the locale identifier

#sentence = raw_input("Enter String: ")

#result = p.parse(sentence)

#print result[0]

#date_str = time.strftime("%Y-%m-%d %H:%M:%S", result[0])
#print date_str

#dt_obj = datetime(*result[0][0:6])
#print dt_obj.isoformat()




